package com.tys.algorithm.advanced.test.class10;

public class Code01_FindFirstIntersectNode {

    //单链表结构
    public static class Node {
        public int value;
        public Node next;

        public Node(int data) {
            this.value = data;
        }
    }

    public static Node getIntersectNode(Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return null;
        }
        //找入环节点
        Node loop1 = getLoopNode(head1);
        Node loop2 = getLoopNode(head2);
        //head1和head2都是无环，相交
        if (loop1 == null && loop2 == null) {
            return noLoop(head1, head2);
        }
        //head1和head2都是有环，相交
        if (loop1 != null && loop2 != null) {
            return bothLoop(head1, loop1, head2, loop2);
        }
        //不相交：head1和head2一个有环一个无环，必定不相交
        return null;
    }

    // 找到链表第一个入环节点，如果无环，返回null
    public static Node getLoopNode(Node head) {
        if (head == null || head.next == null || head.next.next == null) {
            return null;
        }
        // n1 慢  n2 快
        Node slow = head.next; // n1 -> slow 走1步
        Node fast = head.next.next; // n2 -> fast 走2步
        //相等就是相遇：第一次相遇
        while (slow != fast) {
            //快指针结束必定无环
            if (fast.next == null || fast.next.next == null) {
                return null;
            }
            fast = fast.next.next; //2步
            slow = slow.next; //1步
        }
        // slow fast  相遇
        // 快指针回到头，慢指针还在原地
        fast = head; // n2 -> walk again from head
        //相等就是相遇：第二次相遇
        while (slow != fast) {
            slow = slow.next; //1步
            fast = fast.next; //1步
        }
        return slow;
    }

    // 如果两个链表都无环，返回第一个相交节点，如果不想交，返回null
    public static Node noLoop(Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return null;
        }
        Node cur1 = head1;
        Node cur2 = head2;
        int n = 0;
        //找到head1的尾节点，并记录长度
        while (cur1.next != null) {
            n++;
            cur1 = cur1.next;
        }
        //找到head2的尾节点
        //n>0 则 head1长
        //n<0 则 head2长
        while (cur2.next != null) {
            n--;
            cur2 = cur2.next;
        }
        //不相等则不相交
        if (cur1 != cur2) {
            return null;
        }
        // n  :  链表1长度减去链表2长度的值
        //cur1长链表，cur2短链表
        cur1 = n > 0 ? head1 : head2; // 谁长，谁的头变成cur1
        cur2 = cur1 == head1 ? head2 : head1; // 谁短，谁的头变成cur2
        n = Math.abs(n);  //绝对值
        //长链表先走n步
        while (n != 0) {
            n--;
            cur1 = cur1.next;
        }
        //相等相交，cur1和cur2同时移动
        while (cur1 != cur2) {
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur1;
    }

    // 两个有环链表，返回第一个相交节点，如果不想交返回null
    public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
        Node cur1 = null;
        Node cur2 = null;
        //入环节点是同一个
        if (loop1 == loop2) {
            cur1 = head1;
            cur2 = head2;
            int n = 0;
            //找到head1的尾节点，并记录长度
            while (cur1 != loop1) {
                n++;
                cur1 = cur1.next;
            }
            //找到head2的尾节点
            //n>0则head1长
            //n<0则head2长
            while (cur2 != loop2) {
                n--;
                cur2 = cur2.next;
            }
            //cur1长链表，cur2短链表
            cur1 = n > 0 ? head1 : head2;
            cur2 = cur1 == head1 ? head2 : head1;
            n = Math.abs(n);//绝对值
            //长链表先走n步
            while (n != 0) {
                n--;
                cur1 = cur1.next;
            }
            //一起走，相等则相交
            while (cur1 != cur2) {
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            return cur1;
        } else {
            cur1 = loop1.next;
            while (cur1 != loop1) {
                //遇到loop2则相交
                if (cur1 == loop2) {
                    return loop1;
                }
                cur1 = cur1.next;
            }
            //没遇到loop2则不相交
            return null;
        }
    }

    public static void main(String[] args) {
        // 1->2->3->4->5->6->7->null
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);

        // 0->9->8->6->7->null
        Node head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

        // 1->2->3->4->5->6->7->4...
        head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);
        head1.next.next.next.next.next.next = head1.next.next.next; // 7->4

        // 0->9->8->2...
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next; // 8->2
        System.out.println(getIntersectNode(head1, head2).value);

        // 0->9->8->6->4->5->6..
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

    }

}
